<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="format-detection" content="telephone=no">
    <title>无敌螺丝王</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            overflow: hidden;
            /* 移动端优化 */
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            -webkit-tap-highlight-color: transparent;
            touch-action: manipulation;
        }
        
        .game-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
            text-align: center;
            max-width: 800px;
            width: 90%;
        }
        
        .game-title {
            font-size: 3em;
            color: #333;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
            background: linear-gradient(45deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .game-info {
            display: flex;
            justify-content: space-around;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }
        
        .info-item {
            background: #f8f9fa;
            padding: 15px 25px;
            border-radius: 15px;
            margin: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }
        
        .info-item:hover {
            transform: translateY(-2px);
        }
        
        .info-label {
            font-size: 0.9em;
            color: #666;
            margin-bottom: 5px;
        }
        
        .info-value {
            font-size: 1.5em;
            font-weight: bold;
            color: #333;
        }
        
        .game-board {
            background: #2c3e50;
            border-radius: 15px;
            padding: 20px;
            margin: 20px 0;
            min-height: 400px;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
            gap: 15px;
            align-content: start;
        }
        
        .screw-slot {
            width: 80px;
            height: 80px;
            background: #34495e;
            border-radius: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            border: 3px solid #34495e;
            /* 移动端优化 */
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            user-select: none;
            -webkit-tap-highlight-color: transparent;
            touch-action: manipulation;
        }
        
        .screw-slot:active {
            transform: scale(0.95);
        }
        
        .screw-slot:focus {
            outline: none;
        }
        
        .screw-slot:hover {
            background: #4a5f7a;
            transform: scale(1.05);
        }
        
        .screw-slot.filled {
            background: #27ae60;
            border-color: #2ecc71;
        }
        
        .screw-slot.target {
            background: #e74c3c;
            border-color: #c0392b;
            animation: pulse 1s infinite;
        }
        
        .screw {
            width: 50px;
            height: 50px;
            background: linear-gradient(45deg, #95a5a6, #bdc3c7);
            border-radius: 50%;
            position: relative;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            transition: all 0.3s ease;
        }
        
        .screw::before {
            content: '+';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
        }
        
        .screw.golden {
            background: linear-gradient(45deg, #f1c40f, #f39c12);
            box-shadow: 0 0 20px rgba(241, 196, 15, 0.6);
        }
        
        .screw.golden::before {
            color: #8b4513;
            font-size: 28px;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 30px;
            flex-wrap: wrap;
        }
        
        .btn {
            padding: 15px 30px;
            font-size: 1.1em;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            text-transform: uppercase;
            letter-spacing: 1px;
            /* 移动端优化 */
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            user-select: none;
            -webkit-tap-highlight-color: transparent;
            touch-action: manipulation;
        }
        
        .btn:active {
            transform: scale(0.95);
        }
        
        .btn:focus {
            outline: none;
        }
        
        .btn-primary {
            background: linear-gradient(45deg, #3498db, #2980b9);
            color: white;
            box-shadow: 0 4px 15px rgba(52, 152, 219, 0.4);
        }
        
        .btn-primary:hover {
            background: linear-gradient(45deg, #2980b9, #3498db);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(52, 152, 219, 0.6);
        }
        
        .btn-secondary {
            background: linear-gradient(45deg, #95a5a6, #7f8c8d);
            color: white;
            box-shadow: 0 4px 15px rgba(149, 165, 166, 0.4);
        }
        
        .btn-secondary:hover {
            background: linear-gradient(45deg, #7f8c8d, #95a5a6);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(149, 165, 166, 0.6);
        }
        
        .message {
            margin-top: 20px;
            padding: 15px;
            border-radius: 10px;
            font-weight: bold;
            font-size: 1.1em;
            min-height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message.info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.1); }
            100% { transform: scale(1); }
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .screw.rotating {
            animation: rotate 0.5s ease-in-out;
        }
        
        @media (max-width: 768px) {
            .game-container {
                padding: 20px;
                width: 95%;
            }
            
            .game-title {
                font-size: 2.2em;
                margin-bottom: 15px;
            }
            
            .game-info {
                margin-bottom: 20px;
            }
            
            .info-item {
                padding: 10px 15px;
                margin: 3px;
            }
            
            .info-label {
                font-size: 0.8em;
            }
            
            .info-value {
                font-size: 1.2em;
            }
            
            .game-board {
                grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));
                gap: 12px;
                padding: 15px;
                min-height: 350px;
            }
            
            .screw-slot {
                width: 70px;
                height: 70px;
            }
            
            .screw {
                width: 45px;
                height: 45px;
            }
            
            .screw::before {
                font-size: 20px;
            }
            
            .controls {
                gap: 10px;
                margin-top: 20px;
            }
            
            .btn {
                padding: 12px 20px;
                font-size: 1em;
            }
            
            .message {
                font-size: 1em;
                padding: 12px;
            }
        }
        
        @media (max-width: 480px) {
            .game-container {
                padding: 15px;
                width: 98%;
            }
            
            .game-title {
                font-size: 1.8em;
                margin-bottom: 10px;
            }
            
            .game-info {
                margin-bottom: 15px;
            }
            
            .info-item {
                padding: 8px 12px;
                margin: 2px;
            }
            
            .info-label {
                font-size: 0.7em;
            }
            
            .info-value {
                font-size: 1em;
            }
            
            .game-board {
                grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
                gap: 8px;
                padding: 10px;
                min-height: 300px;
            }
            
            .screw-slot {
                width: 60px;
                height: 60px;
            }
            
            .screw {
                width: 40px;
                height: 40px;
            }
            
            .screw::before {
                font-size: 18px;
            }
            
            .controls {
                gap: 8px;
                margin-top: 15px;
            }
            
            .btn {
                padding: 10px 15px;
                font-size: 0.9em;
            }
            
            .message {
                font-size: 0.9em;
                padding: 10px;
            }
        }
        
        @media (max-width: 320px) {
            .game-title {
                font-size: 1.5em;
            }
            
            .game-board {
                grid-template-columns: repeat(auto-fit, minmax(50px, 1fr));
                gap: 6px;
                padding: 8px;
                min-height: 250px;
            }
            
            .screw-slot {
                width: 50px;
                height: 50px;
            }
            
            .screw {
                width: 35px;
                height: 35px;
            }
            
            .screw::before {
                font-size: 16px;
            }
            
            .btn {
                padding: 8px 12px;
                font-size: 0.8em;
            }
        }
    </style>
</head>
<body>
    <div class="game-container">
        <h1 class="game-title">🔧 无敌螺丝王 🔧</h1>
        
        <div class="game-info">
            <div class="info-item">
                <div class="info-label">关卡</div>
                <div class="info-value" id="level">1</div>
            </div>
            <div class="info-item">
                <div class="info-label">分数</div>
                <div class="info-value" id="score">0</div>
            </div>
            <div class="info-item">
                <div class="info-label">移动次数</div>
                <div class="info-value" id="moves">0</div>
            </div>
            <div class="info-item">
                <div class="info-label">时间</div>
                <div class="info-value" id="time">00:00</div>
            </div>
        </div>
        
        <div class="game-board" id="gameBoard">
            <!-- 游戏格子将在这里动态生成 -->
        </div>
        
        <div class="controls">
            <button class="btn btn-primary" onclick="startGame()">开始游戏</button>
            <button class="btn btn-secondary" onclick="resetGame()">重新开始</button>
            <button class="btn btn-secondary" onclick="showHelp()">游戏帮助</button>
        </div>
        
        <div class="message info" id="message">
            点击"开始游戏"来开始你的螺丝王之旅！
        </div>
    </div>

    <script>
        // 游戏状态
        let gameState = {
            level: 1,
            score: 0,
            moves: 0,
            time: 0,
            isPlaying: false,
            board: [],
            targetPositions: [],
            timer: null,
            boardSize: { rows: 4, cols: 5 }
        };

        // 初始化游戏
        function initGame() {
            createBoard();
            updateDisplay();
        }

        // 创建游戏板
        function createBoard() {
            const board = document.getElementById('gameBoard');
            board.innerHTML = '';
            gameState.board = [];
            
            const { rows, cols } = gameState.boardSize;
            
            for (let i = 0; i < rows; i++) {
                gameState.board[i] = [];
                for (let j = 0; j < cols; j++) {
                    const slot = document.createElement('div');
                    slot.className = 'screw-slot';
                    slot.dataset.row = i;
                    slot.dataset.col = j;
                    
                    // 添加点击和触摸事件
                    slot.addEventListener('click', (e) => {
                        e.preventDefault();
                        handleSlotClick(i, j);
                    });
                    
                    slot.addEventListener('touchstart', (e) => {
                        e.preventDefault();
                        handleSlotClick(i, j);
                    }, { passive: false });
                    
                    // 随机放置螺丝
                    const hasScrew = Math.random() < 0.7;
                    if (hasScrew) {
                        const screw = document.createElement('div');
                        const isGolden = Math.random() < 0.1;
                        screw.className = `screw ${isGolden ? 'golden' : ''}`;
                        slot.appendChild(screw);
                        gameState.board[i][j] = isGolden ? 'golden' : 'normal';
                        slot.classList.add('filled');
                    } else {
                        gameState.board[i][j] = null;
                    }
                    
                    board.appendChild(slot);
                }
            }
            
            // 设置目标位置
            setTargetPositions();
        }

        // 设置目标位置
        function setTargetPositions() {
            // 清除之前的目标
            document.querySelectorAll('.screw-slot').forEach(slot => {
                slot.classList.remove('target');
            });
            
            gameState.targetPositions = [];
            const { rows, cols } = gameState.boardSize;
            const targetCount = Math.min(3, Math.floor(rows * cols * 0.2));
            
            for (let i = 0; i < targetCount; i++) {
                let row, col;
                do {
                    row = Math.floor(Math.random() * rows);
                    col = Math.floor(Math.random() * cols);
                } while (gameState.targetPositions.some(pos => pos.row === row && pos.col === col));
                
                gameState.targetPositions.push({ row, col });
                const slot = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
                slot.classList.add('target');
            }
        }

        // 处理格子点击
        function handleSlotClick(row, col) {
            if (!gameState.isPlaying) return;
            
            const slot = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
            const screw = slot.querySelector('.screw');
            
            if (screw) {
                // 如果有螺丝，移除它
                screw.classList.add('rotating');
                setTimeout(() => {
                    screw.remove();
                    slot.classList.remove('filled');
                    gameState.board[row][col] = null;
                    gameState.moves++;
                    
                    // 检查是否达到目标
                    checkTargets();
                    updateDisplay();
                }, 500);
            } else {
                // 如果没有螺丝，添加一个
                const newScrew = document.createElement('div');
                const isGolden = Math.random() < 0.1;
                newScrew.className = `screw ${isGolden ? 'golden' : ''}`;
                slot.appendChild(newScrew);
                slot.classList.add('filled');
                gameState.board[row][col] = isGolden ? 'golden' : 'normal';
                gameState.moves++;
                
                // 检查是否达到目标
                checkTargets();
                updateDisplay();
            }
        }

        // 检查目标完成情况
        function checkTargets() {
            let completedTargets = 0;
            
            gameState.targetPositions.forEach(pos => {
                const slot = document.querySelector(`[data-row="${pos.row}"][data-col="${pos.col}"]`);
                if (!slot.querySelector('.screw')) {
                    completedTargets++;
                }
            });
            
            if (completedTargets === gameState.targetPositions.length) {
                // 关卡完成
                levelComplete();
            }
        }

        // 关卡完成
        function levelComplete() {
            gameState.isPlaying = false;
            clearInterval(gameState.timer);
            
            const bonus = Math.max(0, 1000 - gameState.moves * 10 - gameState.time * 5);
            gameState.score += bonus;
            
            showMessage(`🎉 关卡完成！获得奖励分数：${bonus}`, 'success');
            
            setTimeout(() => {
                gameState.level++;
                gameState.moves = 0;
                gameState.time = 0;
                
                // 增加难度
                if (gameState.level % 3 === 0) {
                    gameState.boardSize.rows = Math.min(6, gameState.boardSize.rows + 1);
                    gameState.boardSize.cols = Math.min(8, gameState.boardSize.cols + 1);
                }
                
                createBoard();
                updateDisplay();
                showMessage(`进入第 ${gameState.level} 关！`, 'info');
            }, 2000);
        }

        // 开始游戏
        function startGame() {
            gameState.isPlaying = true;
            gameState.moves = 0;
            gameState.time = 0;
            gameState.score = 0;
            gameState.level = 1;
            gameState.boardSize = { rows: 4, cols: 5 };
            
            createBoard();
            updateDisplay();
            startTimer();
            showMessage('游戏开始！清除红色目标位置的螺丝！', 'info');
        }

        // 重置游戏
        function resetGame() {
            gameState.isPlaying = false;
            clearInterval(gameState.timer);
            gameState.level = 1;
            gameState.score = 0;
            gameState.moves = 0;
            gameState.time = 0;
            gameState.boardSize = { rows: 4, cols: 5 };
            
            createBoard();
            updateDisplay();
            showMessage('游戏已重置，点击"开始游戏"重新开始！', 'info');
        }

        // 显示帮助
        function showHelp() {
            showMessage('🎯 游戏目标：清除红色目标位置的所有螺丝！🔧 点击格子可以添加或移除螺丝。金色螺丝价值更高！', 'info');
        }

        // 开始计时器
        function startTimer() {
            gameState.timer = setInterval(() => {
                gameState.time++;
                updateDisplay();
            }, 1000);
        }

        // 更新显示
        function updateDisplay() {
            document.getElementById('level').textContent = gameState.level;
            document.getElementById('score').textContent = gameState.score;
            document.getElementById('moves').textContent = gameState.moves;
            
            const minutes = Math.floor(gameState.time / 60);
            const seconds = gameState.time % 60;
            document.getElementById('time').textContent = 
                `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        }

        // 显示消息
        function showMessage(text, type = 'info') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
        }

        // 页面加载完成后初始化游戏
        window.onload = initGame;
    </script>
</body>
</html>